package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.PermissionsAliasConstants;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeletePermissionOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PermissionDTO;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Autowired
    private RolePermissionService permissionService;

    // 添加账号权限
    @SaAdminCheckPermission(PermissionsAliasConstants.Permission.ADD)
    @PostMapping
    public Result<?> addPermission(@RequestBody PermissionCommand command) {
        permissionService.savePermissions(command);
        return Result.ok();
    }

    // 修改账号权限
    @SaAdminCheckPermission(PermissionsAliasConstants.Permission.UPDATE)
    @PutMapping
    public Result<?> updatePermission(@RequestBody PermissionCommand command) {
        permissionService.updatePermission(command);
        return Result.ok();
    }

    // 账号权限列表（权限树）
    @SaAdminCheckPermission(PermissionsAliasConstants.Permission.SEARCH)
    @GetMapping("/list")
    public Result<List<PermissionDTO>> getPermissionList() {
        List<PermissionDTO> list = permissionService.getPermissions();
        return Result.ok(list);
    }

    // 删除指定账号权限
    @SaAdminCheckPermission(PermissionsAliasConstants.Permission.DELETE)
    @DeleteMapping("/{permissionId}")
    public Result<DeletePermissionOperateDTO> deletePermission(@PathVariable Long permissionId) {
        DeletePermissionOperateDTO dto = permissionService.deletePermission(permissionId);
        return Result.ok(dto);
    }
}